package com.example.myapplication1;

import android.content.ContentUris;
import android.content.Context;
import android.util.Log;

import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.ext.junit.runners.AndroidJUnit4;

import com.example.myapplication1.threademo.productinterface.Custom;
import com.example.myapplication1.threademo.productinterface.Product;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
 * Instrumented test, which will execute on an Android device.
 *
 * @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
 */
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {


    private static final String TAG = "ThreadTest";
    private int MAX_COUNT = 10;
    //    目前的数量
    private int currentCount;
    //    锁
    private Object object = new Object();

    @Test
    public void useAppContext() {
        // Context of the app under test.
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

        assertEquals("com.example.myapplication1", appContext.getPackageName());
    }


    @Test
    public void test() {

        for (int i =0; i < 10;i++){
            Customer customer = new Customer();
            customer.start();
        }

        for (int i =0; i < 4;i++){
            Producter product = new Producter();
            product.start();
        }
    }


    public class Customer extends Thread implements Custom {

        @Override
        public void custom() {
//  消费者 如果目前的数量小于0 ,就等待,否者就消费一个

            while (true) {
                synchronized (object) {
                    try {
                        sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (currentCount <= 0) {
                        try {
                            object.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        Log.e("tag", "目前没有可消费的苹果,等待");

                    } else {

                        currentCount--;
                        Log.e("tag", "消费者消费了1个苹果,剩余" + currentCount + "个苹果");
                        object.notifyAll();
                    }
                }
            }
        }

        @Override
        public void run() {
            super.run();
            custom();
        }
    }

    public class Producter extends Thread implements Product {


        public void product() {
//             生产者 如果目前的苹果大于最大数量，等待消费者消费后在生产，如果生产的苹果加上剩下的苹果大于了最大数量，应该就生产最大数量
            while (true) {
                try {
                    sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (object) {
                    if (currentCount >= MAX_COUNT) {

                        try {
                            object.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        Log.e("tag", "生产的苹果已经达到最大数量了，等消费者消费");
                    } else {
                        currentCount += Math.random() * 10;
                        Log.e("tag", "生产了" + currentCount + "个苹果");
                        object.notifyAll();
                    }
                }
            }

        }

        @Override
        public void run() {
            super.run();
            product();

        }
    }
}
